<#include "/layout/template.ftl">
<@header></@header>

<div>
    <!-- <blockquote class="layui-elem-quote">新增用户</blockquote> -->
    <div class="layui-inline">
        <form class="layui-form" method="post">
            <div class="layui-form-item">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-block">
                    <input type="text" name="username" lay-verify="username_lay" autocomplete="off" placeholder="请输入用户名" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-block">
                    <input type="password" name="password" lay-verify="userpass_lay" placeholder="请输入密码" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">手机号</label>
                <div class="layui-input-block">
                    <input type="text" name="mobile" lay-verify="userphone_lay" placeholder="请输入手机号" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="userState" lay-skin="switch" lay-text="正常|异常" checked="checked">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="refer">提交</button>
                </div>
            </div>
        </form>
    </div>
</div>
<@footer>
<script>
    layui.use(['form'], function() {
        var form = layui.form,layer = layui.layer,$=layui.$;
        //自定义验证规则
    /*     form.verify({
            username_lay: function(value) {
                if(value.length < 3) {
                    return '用户名至少得3个字符啊';
                }
            },
            userpass_lay: [/(.+){6,12}$/, '密码必须6到12位'],
            userphone_lay: [/^1[3|4|5|8][0-9]\d{4,8}$/, '手机号格式错误']
        }); */
        //监听指定开关
        form.on('switch(userState)', function(data) {
            layer.msg('开关checked：' + (this.checked ? 'true' : 'false'), {
                offset: '6px'
            });
            layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
        });
        //监听提交
        form.on('submit(refer)', function(data) {
            if(data.field.userState == "on") {
                data.field.userState = "enabled";
            } else {
                data.field.userState = "disabled";
            }
            var userobj = new Object();
            userobj.userName = data.field.username;
            userobj.userPass = data.field.userpass;
            userobj.userPhone = data.field.userphone;
            userobj.userState = data.field.userState;
            var json = JSON.stringify(userobj);
            //var loading = layer.load(1);
           $.ajax({
                url: "/user/add",
                dataType: "text",
                contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                data: layui.$('.layui-form').serialize(),
                type: "post",
                success: function (json) {
                	result=JSON.parse(json);
                	if (result.status == 200) {
                        if(result.message){
                        	layer.msg(result.message, {
                                time: 3000, 
                                icon: 6,
                                btn: ['确定']
                             },function(){
                            	 parent.layer.close(parent.layer.getFrameIndex(window.name));  
                            	 //window.parent.layer.closeAll();
                             });
                        }
                    } else {
                        if(result.message){
                        	layer.msg(result.message, {
                                icon: 5
                            });
                        }
                    }
                	
                	
                },
                error: function () {
                    layer.msg("添加失败", {
                        icon: 5
                    });
                }
            });
            return false;
            // layer.alert(JSON.stringify(data.field), {
            //     title: '最终的提交信息'
            // })
            // return false;
        });
    });
</script>
   
</@footer>
